<template>
  <div class="renovation-list views-container">
    <el-dialog :visible.sync="dialogVisibleImg" width="33%" custom-class="reset-pwd wlm-form-dialog dialog-table">
      <div class="wlm-form">
        <div class="wlm-form-header wlm-dialog-fixheader">
          <span class="wlm-dialog-inlenr">页面推广</span>
        </div>
        <div class="flex-col flex-align-c flex-justify-c" style="margin:20px 0">
          <img height="220" width="220" style="margin:10px 0 30px" v-if="previewCopy.h5_qrcode" :src="`data:image/png;base64,${previewCopy.h5_qrcode}`" alt="">
          <!-- <div class="flex-col"> -->
          <p style="margin-bottom:20px">扫一扫预览页面</p>
          <el-input size="small" :disabled="true" v-model="previewCopy.url" style="width:330px;margin-bottom:10px;" v-if="previewCopy.url">
            <el-button slot="append" type="primary" @click="handleClipboard(previewCopy.url,$event)">复制路径</el-button>
          </el-input>
          <el-input size="small" :disabled="true" v-model="previewCopy.h5_url" style="width:330px;margin-bottom:10px;" v-if="previewCopy.h5_url">
            <el-button slot="append" type="primary" @click="handleClipboard(previewCopy.h5_url,$event)">复制链接</el-button>
          </el-input>
           <el-input size="small" :disabled="true" v-model="previewCopy.h5_short_url" style="width:330px;" v-if="previewCopy.h5_short_url">
            <el-button slot="append" type="primary" @click="handleClipboard(previewCopy.h5_short_url,$event)">复制短链接</el-button>
          </el-input>
          <!-- </div> -->
        </div>
      </div>
    </el-dialog>
    <div class="wlm-table">
      <div class="wlm-table-hearder-btn " style="display:flex;justify-content:space-between;">
        <router-link tag="span" to="/platform/renovationHome">
          <el-button size="small" type="primary">新建页面</el-button>
        </router-link>
        <div v-if="$agent">
          是否需要开启多渠道：
          <el-radio-group v-model="open" @change="changeOpen" >
            <el-radio label="1">是</el-radio>
            <el-radio label="2">否</el-radio>
          </el-radio-group>
        </div>

      </div>
      <div class="wlm-table-header">
        <el-form :model="tableFormatData.userTable.files" size="small" label-width="80px" class="retail-form" label-position="right">
          <el-form-item label="" class="filter">
            <el-form-item label="页面名称：">
              <el-input @keyup.enter.native="filesSerch" v-model="tableFormatData.userTable.files.name" placeholder="请输入页面标题" style="width:160px;"></el-input>
            </el-form-item>
            <el-form-item label="页面类型：" label-width="100px">
              <el-select placeholder="请选择" v-model="tableFormatData.userTable.files.type">
                <el-option v-for="item in [{lable:'所有',value:'0'},{lable:'自定义页面',value:'1'},{lable:'平台首页',value:'2'}]" :key="item.value" :label="item.lable" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-form-item>
          <el-form-item label="" class="filter-btns">
            <el-button type="primary" @click="filesSerch">筛选</el-button>
            <el-button type="text" @click="filesEmpty">清空筛选条件</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="wlm-table-content">
        <el-table :ref="tableFormatData.userTable.key" :data="tableFormatData.userTable.tableData" @selection-change="handleSelectionChange"> style="width: 100%">
          <el-table-column type="selection" width="55">
          </el-table-column>
          <el-table-column prop="date" label="页面名称">
            <template slot-scope="scope">
              <span>{{scope.row.name}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="date" label="页面类型">
            <template slot-scope="scope">
              <!-- <router-link tag="span" :to="{path:'/user/managementUserEdit', query:{id:scope.row.id}}">
              <span>{{pageType[`${scope.row.type}`]}}</span>
            </router-link> -->
              <span>{{pageType[`${scope.row.type}`]}}</span>
              <img v-if="scope.row.source && scope.row.type==2 && open==1" height="20" width="20" :src="sourceImg[`${scope.row.source || '-'}`]" alt />
            </template>
          </el-table-column>
          <el-table-column prop="name" label="修改时间">
            <template slot-scope="scope">
              <span>{{scope.row.update_time}}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <div class="operation-group">
                <el-button class="wlm-text" type="text" @click="previewDialog(scope.row.diy_page_id)">推广</el-button>
                <div class="btn-line"></div>
                <router-link class="wlm-text" tag="a" target="_blank" :to="{path:'/platform/renovationHome', query:{
                  diy_page_id:scope.row.diy_page_id,
                  type: scope.row.type,
                  store_user_id:$route.query.store_user_id?$route.query.store_user_id:'',
                  h_aid:$route.query.h_aid?$route.query.h_aid:'',
                  }}">
                  <el-button type="text">编辑</el-button>
                </router-link>
                <div class="btn-line"></div>
                <el-button class="wlm-text" type="text" @click="changeTableItem({diy_page_id: scope.row.diy_page_id},tableFormatData.userTable.change.copy)">复制</el-button>
                <div class="btn-line"></div>
                <el-button class="wlm-text" type="text" @click="delTableItem(scope.row.diy_page_id)">删除</el-button>
                <template v-if="`${scope.row.type}` !== '2'">
                  <div class="btn-line"></div>
                  <el-button class="wlm-text" type="text" @click="toggle({diy_page_id: scope.row.diy_page_id,type: 2})">设为首页</el-button>
                </template>
                <template v-if="`${scope.row.type}` == '2' && open==1">
                  <div class="btn-line"></div>
                  <el-button class="wlm-text" type="text" @click="toggle({diy_page_id: scope.row.diy_page_id,type: 2})">更换渠道</el-button>
                </template>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-content flex-row flex-justify-b flex-align-c">
          <el-row class="pagination-btns">
            <el-checkbox class="check-all" true-label="1" false-label="0" v-model="tableFormatData.userTable.files.checkall" @change="toggleSelection">全部</el-checkbox>
            <el-button class="right-8" :disabled="isGroup" size="mini" @click="delTableList">删除</el-button>
          </el-row>
          <el-pagination :disabled="!hasTableData" @size-change="listPageChange" @current-change="listPageChange" 
          :current-page.sync="tableFormatData.userTable.pagination.page" :page-sizes="tableFormatData.userTable.pagination.pagesizes" 
          :page-size.sync="tableFormatData.userTable.pagination.list_rows" layout="total, sizes, prev, pager, next, jumper"
           :total="hasTableData?tableFormatData.userTable.pagination.total : 0">
          </el-pagination>
        </div>
      </div>
    </div>
    <el-dialog :visible.sync="dialogVisibleSource" width="30%" custom-class="reset-pwd wlm-form-dialog dialog-table">
      <div class="wlm-form" v-if="dialogVisibleSource">
        <div class="wlm-form-header wlm-dialog-fixheader">
          <span class="wlm-dialog-inlenr">选择渠道</span>
        </div>
        <el-scrollbar wrap-class="scrollbar-wrapper">
          <div class="wlm-form-content">
            <div class="wlm-dialog-main">
              <div class="wlm-dialog-content">
                <el-form ref="userForm" :model="formData" style="width:200px;" label-width="120px" class="retail-form" label-position="right">
                  <el-form-item label="选择渠道">
                    <el-radio-group v-model="formData.source">
                      <el-radio label="2" v-if="JSON.stringify($store.getters.power).indexOf('wxapp')>-1">微信小程序</el-radio>
                      <el-radio label="3" v-if="JSON.stringify($store.getters.power).indexOf('H5')>-1">H5</el-radio>
                      <el-radio label="4" v-if="JSON.stringify($store.getters.power).indexOf('wechat')>-1">微信公众号</el-radio>
                      <el-radio label="5" v-if="JSON.stringify($store.getters.power).indexOf('android')>-1">安卓</el-radio>
                      <el-radio label="6" v-if="JSON.stringify($store.getters.power).indexOf('ios')>-1">苹果</el-radio>
                      <el-radio label="7" v-if="JSON.stringify($store.getters.power).indexOf('aliapp')>-1">支付宝小程序</el-radio>
                      <el-radio label="9" v-if="JSON.stringify($store.getters.power).indexOf('toutiaoapp')>-1">抖音-头条</el-radio>
                      <el-radio label="8" v-if="JSON.stringify($store.getters.power).indexOf('baiduapp')>-1">百度小程序</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-form>
              </div>
            </div>
          </div>
        </el-scrollbar>
      </div>
      <footer class="flex-row flex-align-c flex-justify-c wlm-dialog-fixfooter">
        <el-button size="small" type="primary" @click="save">确定</el-button>
      </footer>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDiyPageList,
  delDiyPageInfo,
  setDiyPageUse,
  preview,
  copyPage,
  applicationSourceOpen,
  sourceOpenSave
} from '@/api/platform'
import Cookies from 'js-cookie'
import mixins from '@/mixins/mixins'
import clipboard from '@/utils/clipboard'
// import { zipArray } from '@/utils/index'
export default {
  mixins: [mixins.getters('Table')],
  name: 'RenovationList',
  components: {
  },
  computed: {
      $agent(){
              //获取cooking中的agent_id
              return !Cookies.get("agent_id")
            }
  },
  data() {
    return {
      open: '1',
      params: { diy_page_id: '', type: '', source: [] },
      formData: {
        source: ''
      },
      dialogVisibleSource: false,
      previewID: '',
      previewCopy: {},
      dialogVisibleImg: false,
      pageType: {
        '1': '自定义页面',
        '2': '平台首页'
      },
      tableFormatData: {
        current: 'userTable',
        userTable: {
          key: 'userTable',
          api: {
            getList: getDiyPageList,
            delList: delDiyPageInfo
          },
          tableData: [],
          files: {
            nickname: '',
            status: '',
            timeArea: [],
            ids: [],
            checkall: '0',
            redirect: 'diy_page_id'
          },
          change: {
            setdiy: {
              name: '编辑',
              key: 'status',
              api: setDiyPageUse
            },
            copy: {
              name: '复制',
              key: 'copy',
              api: copyPage
            }
          },
          options: {
            status: [{
              lable: '全部',
              value: ''
            }, {
              lable: '正常',
              value: 'normal'
            },
            {
              lable: '禁用',
              value: 'hidden'
            }
            ]
          },
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      }
    }
  },
  created() {
    // console.log(zipArray(['a', 'b'], [1, 2, 3, 4]))
  },
  mounted() {
    this.getIsOpen()
  },
  methods: {
    getIsOpen() {
      applicationSourceOpen().then(res => {
        this.open = res.data.data.is_more_source
      })
    },
    changeOpen() {
      sourceOpenSave({ is_more_source: this.open }).then(res => {
        const { code } = res.data
        code == 1 && this.$message.success('操作成功')
      })
    },
    toggle({ diy_page_id, type }) {
      if (this.open == 1) {
        this.dialogVisibleSource = true
        this.params.source = ''
        this.params = { diy_page_id, type, source: '' }
      } else {
        this.changeTableItem({ diy_page_id, type }, this.tableFormatData.userTable.change.setdiy)
      }
    },
    save() {
      this.params.source = this.formData.source
      if (!this.params.source) {
        this.$message.warning('请选择渠道')
        return false
      }
      this.changeTableItem(this.params, this.tableFormatData.userTable.change.setdiy)
      this.dialogVisibleSource = false
    },
    handleClipboard(text, event) {
      clipboard(text, event)
    },
    previewDialog(id) {
      preview({ diy_page_id: id }).then((response) => {
        const {
          data: {
            data: msgData = {}
          }
        } = response
        this.previewCopy = msgData
      })
      this.dialogVisibleImg = true
    }
  }
}
</script>

<style lang="scss" scoped>
.renovation-list {
  /deep/ .el-input-group__append {
    background: #409eff;
    color: #ffffff;
    border: 1px solid #409eff;
  }
}
</style>
